// JavaScript Document
jQuery.fn.updateDecision=function(url){
	var note=$("#resultNote").val();
	var boxes=$(".decisionBox:checked");
	var choiceIds=new Array();
	for(var i=0;i<boxes.length;i++){
		var box=$(boxes[i]);
		choiceIds[i]=box.val();
	}
	var data={
		choices:choiceIds.toLocaleString(),
		note:note,
	};
	var updateUrl="updateDecision?url="+url;
	$.ajax({
		type:"post",
		async:true,
		url:updateUrl,
		data:data,
		success:function(result){
			if(result=="true"){
				$().showMessage("Save successes.");
			}else{
				$().showError("Save fails.");
			}
		},	
	});
};

jQuery.fn.sendDecision=function(url){
	var sendUrl="sendResult";
	var title=$("#title").val();
	var mailTo=$("#mailTo").val();
	var proposerMail=$("#proposerMail").html();
	var data={
		proposerMail:proposerMail,
		url:url,
		title:title,
		mailTo:mailTo,
	};
	$.ajax({
		type:"post",
		async:true,
		url:sendUrl,
		data:data,
		success:function(result){
			if(result=="true"){
				$().showMessage("The decision of the arrangement sent.");
			}else{
				$().showError("Fail sending The decision of the arrangement!");
			}
		},
	});
};

jQuery.fn.fillTotal=function(){
	var totalArray=$('.hiddenTotal');
	for(var i=0;i<totalArray.length;i++){
		var l=totalArray[i];
		var label=$(l);
		var labelId=label.attr("id");
		var value=label.html();
		var choiceId=labelId.substr(11);
		var choiceId2="total"+choiceId;
		$("#"+choiceId2).html(value);
	}
};

jQuery.fn.updateCloseInfo=function(closeTime){
	$("#closeTimeText").html(closeTime);
};

jQuery.fn.closeEvent=function(url){
	var closeLink="closeEvent?url="+url;
	$.ajax({
		type:"get",
		async:false,
		url:closeLink,
		success:function(data){
			if(data[0]=='T'){
				var closeTime=data.substr(1);
				$().updateCloseInfo(closeTime);
				$().updateCloseView();
			}
		},
	});
};

jQuery.fn.highlightRecommend=function(){
	var labels=$(".recommendIdLabel");
	var checkBoxes=$(".decisionBox");
	var decisionDivs=$(".decisionDiv");
	if(labels.length!=0){
		for(var i=0;i<labels.length;i++){
			for(var j=0;j<checkBoxes.length;j++){
				if($(labels[i]).html()==$(checkBoxes[j]).val()){
					$(decisionDivs[j]).css("background-color","#147DB6");
					break;
				}
			}
		}
	}
};

jQuery.fn.updateCloseView=function(){
	$().highlightRecommend();
	$("#total").fadeIn();
	$("#closeButton").hide();
	$("#reopenButton").show();
	$("#resultNoteDiv").fadeIn();
	$("#decision").fadeIn();
	$("#saveButton").show();
	$("#closeTimeLi").fadeIn();
	$("#mailToP").fadeIn();
	$("#sendButton").show();
};

jQuery.fn.reopenEvent=function(url){
	var closeLink="closeEvent?url="+url+"&close=0";
	$.ajax({
		type:"get",
		async:false,
		url:closeLink,
		success:function(data){
			if(data[0]=='T'){
				$().updateReopenView();
			}
		},
	});
};

jQuery.fn.updateReopenView=function(){
	$("#total").fadeOut();
	$("#closeButton").show();
	$("#reopenButton").hide();
	$("#resultNoteDiv").fadeOut();
	$("#decision").fadeOut();
	$("#saveButton").hide();
	$("#closeTimeLi").fadeOut();
	$("#mailToP").fadeOut();
	$("#sendButton").hide();
};

jQuery.fn.showMessage=function(msg){
	$("#messageDiv").html(msg);
	$("#messageDiv").hide();
	$("#messageDiv").fadeIn(1000);
	$("#messageDiv").fadeOut(3000);
};

jQuery.fn.showError=function(error){
	$("#errorMsgDiv").html(error);
	$("#errorMsgDiv").hide();
	$("#errorMsgDiv").fadeIn(1000);
	$("#errorMsgDiv").fadeOut(10000);
};



//<<<<<<<<<<jun>>>>>>>>>>>>>>//
jQuery.fn.proposerAutoComplete_email = function(url){
  var email=$("#proposer_email").val();
  var autoCompleteUrl="autoComplete?url="+url+"&email="+email;
	$.ajax({
		type:"get",
		async:false,
		url:autoCompleteUrl,
		success:function(data){
		    if(data[0]!='-'){
		        if(data[0]!='-'){
		        var name = data;
		        $("#proposer_name").html(data);
			    }
			}
		},	
	});
};


jQuery.fn.proposerAutoComplete_name = function(url){
  var name=$("#proposer_name").val();
  var autoCompleteUrl="autoComplete?url="+url+"&name="+name;
	$.ajax({
		type:"get",
		async:false,
		url:autoCompleteUrl,
		success:function(data){
		    if(data[0]!='-'){
		        var email = data;
		        $("#proposer_email").html(data);
			}
		},
	});
};


jQuery.fn.add_event_receiver1 = function(){
  var scount = $("#hidden_count").val();
  var nscount = scount+"1";
  $("#hidden_count").val(nscount);
  var len = nscount.length;
  var tr_id = "tr_"+len;
//  var name_id = "receiver_name_"+len;
  var email_id = "receiver_email_"+len;
  var remove_id = "receiver_remove_"+len;
  $("#receiver_Table_body").append("<tr id = \""+tr_id+"\"><td><input type=\"text\" class=\"receiver_email_class\" id = \""+email_id+"\"/></td><td><input type=\"button\" class=\"receiver_remove_class\" value=\"remove\" id = \""+remove_id+"\"/></td></tr>");
  //<td><input type=\"text\" class=\"receiver_name_class\" id = \""+name_id+"\"/></td>
  $("#"+remove_id).click(function(){
  $("#"+tr_id).remove();});
};


jQuery.fn.add_event_receiver2 = function(){
  var scount = $("#hidden_count").val();
  var nscount = scount+"1";
  $("#hidden_count").val(nscount);
  var len = nscount.length;
  var tr_id = "tr_"+len;
//  var name_id = "receiver_name_"+len;
  var email_id = "receiver_email_"+len;
  var remove_id = "receiver_remove_"+len;
  $("#receiver_Table_body").append("<tr id = \""+tr_id+"\"><td><input type=\"text\" class=\"receiver_email_class_2\" id = \""+email_id+"\"/></td><td><input type=\"button\" class=\"receiver_remove_class\" value=\"remove\" id = \""+remove_id+"\"/></td></tr>");
  //<td><input type=\"text\" class=\"receiver_name_class\" id = \""+name_id+"\"/></td>
  $("#"+remove_id).click(function(){
  $("#"+tr_id).remove();});
};


jQuery.fn.add_event_choice = function(){
  var scount = $("#hidden_count").val();
  var nscount = scount+"1";
  $("#hidden_count").val(nscount);
  var len = nscount.length;
  
  var date_id1 = "event_choice_date_"+len;
  var date_id2 = "event_choice1_"+len;
  var date = $("#event_choice_date_1").val();
  
  var start_id1 = "event_choice_start_"+len;
  var start_id2 = "event_choice2_"+len;
  var start = $("#event_choice_start_1").val();
  
  var end_id1 = "event_choice_end_"+len;
  var end_id2 = "event_choice3_"+len;
  var end = $("#event_choice_end_1").val();
  
  var remove_id1 = "event_choice_remove_"+len;
  var remove_id2 = "event_choice4_"+len;
  
  $("#event_choice_date").append("<td class=\"event_choice_outer\" id=\""+date_id1+"\"><input type=\"text\" class=\"event_choice_inner1\" id=\""+date_id2+"\" value="+date+"></td>");
  $("#event_choice_start").append("<td class=\"event_choice_outer\" id=\""+start_id1+"\"><input type=\"text\" class=\"event_choice_inner2\" id=\""+start_id2+"\" value="+start+"></td>");
  $("#event_choice_end").append("<td class=\"event_choice_outer\" id=\""+end_id1+"\"><input type=\"text\" class=\"event_choice_inner3\" id=\""+end_id2+"\" value="+end+"></td>");
  $("#event_choice_remove").append("<td class=\"event_choice_outer\" id=\""+remove_id1+"\"><input type=\"button\" class=\"event_choice_inner\" id=\""+remove_id2+"\" value=\"remove\"/></td>");
  
  $("#"+remove_id2).click(function(){
	    $("#"+date_id1).remove();
	    $("#"+start_id1).remove();
	    $("#"+end_id1).remove();
	    $("#"+remove_id1).remove();
	});
};


jQuery.fn.save_event = function(){
	var pemail=$("#proposer_email").val();
	var title = $("#title").val();
	var location = $("#location").val();
	var description = $("#description").val();
	var eventurl = $("#hidden_url").val();
	var createtime = $("#hidden_time").val();
	
	var eventchoices =new Array();
	var datelist =$(".event_choice_inner1");
	var startlist = $(".event_choice_inner2");
	var endlist = $(".event_choice_inner3");
	
	var count = 0;
	for(var i=0;i<datelist.length;i++){
		var date=$(datelist[i]).val();
		var start = $(startlist[i]).val();
		var end = $(endlist[i]).val();
		if(date.length == 0)
		    continue;
		if(start.length == 0)
		    continue;
		if(end.length == 0)
		    continue;    
		var time = date+"+"+start+"+"+end;
		eventchoices[count++] = time;
	}
		
	var data={
		pemail: pemail,
		title: title,
		location: location,
		description: description,
		createtime: createtime,
		eventurl: eventurl,
		eventchoices: eventchoices.toLocaleString(),
	};
	
	var addEventUrl="AddEvent";
	$.ajax({
		type:"post",
		async:false,
		url:addEventUrl,
		data:data,
		success:function(result){
			if(result=="success"){
				$().showMessage("Save successes.");
			}else{
				$().showMessage("Save fails.");
			}
		},	
	});
};


jQuery.fn.save_and_send_event = function(){
	var pemail=$("#proposer_email").val();
	var title = $("#title").val();
	var location = $("#location").val();
	var description = $("#description").val();
	var eventurl = $("#hidden_url").val();
	var createtime = $("#hidden_time").val();
	
	var eventchoices =new Array();
	var datelist =$(".event_choice_inner1");
	var startlist = $(".event_choice_inner2");
	var endlist = $(".event_choice_inner3");
	
	var count = 0;
	for(var i=0;i<datelist.length;i++){
		var date=$(datelist[i]).val();
		var start = $(startlist[i]).val();
		var end = $(endlist[i]).val();
		if(date.length == 0)
		    continue;
		if(start.length == 0)
		    continue;
		if(end.length == 0)
		    continue;  
		    
		var time = date+"+"+start+"+"+end;
		eventchoices[count++] = time;
	}
	
	var receivers =new Array();
	var emails =$(".receiver_email_class");
	if(emails.length == 0)
	{
	    emails = $(".receiver_email_class_2");
	}
	
	count = 0;
	for(var i=0;i<emails.length;i++){
		var email=$(emails[i]).val();
		if(email.length == 0)
		    continue;
		receivers[count++] = email;
	}
	
	var data={
		pemail: pemail,
		title: title,
		location: location,
		description: description,
		createtime: createtime,
		eventurl: eventurl,
		eventchoices: eventchoices.toLocaleString(),
		receivers: receivers.toLocaleString(),
	};
	
	var addEventSendUrl="AddEventSend";
	$.ajax({
		type:"post",
		async:false,
		url:addEventSendUrl,
		data:data,
		success:function(result){
			if(result=="success"){
				$().showMessage("Save and Send Success.");
			}else{
				$().showMessage("Save fails.");
			}
		},	
	});
};


function contactAddFunction(contactemail)
{
  var receiverlist = $(".receiver_email_class");
  var receiversize = receiverlist.length;
  for(var j = 0; j < receiversize; j++)
  {
     var receiver = $(receiverlist[j]);
     var receivermail = receiver.val();
     if(receivermail.length==0)
     {
          receiver.val(contactemail);
          return;
     }
  }
  
  var scount = $("#hidden_count").val();
  var nscount = scount+"1";
  $("#hidden_count").val(nscount);
  var len = nscount.length;
  
  var tr_id = "tr_"+len;
  var email_id = "receiver_email_"+len;
  var remove_id = "receiver_remove_"+len;
  $("#receiver_Table_body").append("<tr id = \""+tr_id+"\"><td><input type=\"text\" class=\"receiver_email_class\" id = \""+email_id+"\" value = \""+contactemail+"\"/></td><td><input type=\"button\" class=\"receiver_remove_class\" value=\"remove\" id = \""+remove_id+"\"/></td></tr>");
  $("#"+remove_id).click(function(){
  $("#"+tr_id).remove();});  
}

$(document).ready(function() {
	$().fillTotal();
	var url=$("#eventUrl").html();
	$("#closeButton").click(function(){
		$().closeEvent(url);
	});
	$("#reopenButton").click(function(){
		$().reopenEvent(url);
	});
	$("#saveButton").click(function(){
		$().updateDecision(url);
	});
	$("#sendButton").click(function(){
		$().sendDecision(url);
	});
	var isClose=$("#isClosedLabel").html();
	if(isClose=="true"){
		$().updateCloseView();
	}
	//$().showMeta(metadata);
	
	
	//<<<<<<<<<<<<<jun>>>>>>>>>>>>>>>>>>//	
		$("proposer_name").keydown(function(){
		    if(event.which == 13)
		    {
		       $().proposerAutoComplete_name(url);
		    }
		});
		
		$("propaser_email").keydown(function(){
	    	if(event.which == 13)
		    {
		       $().proposerAutoComplete_email(url);
		    }
		});
		
		$("#add_event_choice_Button").click(function(){
	        $().add_event_choice();
	    });
		
		$("#add_receiver_Button1").click(function(){
		    $().add_event_receiver1();
		});
		
		$("#add_receiver_Button2").click(function(){
		    $().add_event_receiver2();
		});
		
		$("#receiver_remove_1").click(function(){
		    $("#receiver_email_1").val("");
		});
		
		$("#save_Button").click(function(){
		    $().save_event();
		});
		
		$("#save_send_Button").click(function(){
		    $().save_and_send_event();
		});
});
